<%--
  Created by IntelliJ IDEA.
  Dept: Gao
  Date: 2024/1/8
  Time: 14:23
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
    <title>Title</title>
    <%@ include file="header.jsp"%>
</head>
<body>
    <%--${list}--%>
    <form class="form-inline" action="/dept?method=selectAll" method="post">
        <div class="form-group">
            <label>用户名</label>
            <input type="text" class="form-control"  name="name"/>
        </div>
        <input class="btn btn-primary" type="submit" value="搜索"/>
        <a class="btn btn-sm btn-primary" href="/dept_add.jsp">添加</a>
        <a class="btn btn-sm btn-danger" href="javascript:deleteAll()">批量删除</a>
    </form>

    <table class="table table-striped table-bordered table-hover table-condensed">
        <tr>
            <td><input type="checkbox" id="checkAll"/></td>
            <td>ID</td>
            <td>部门名</td>
            <td>地址</td>
            <td>电话</td>
            <td>删除</td>
            <td>编辑</td>
        </tr>
        <c:forEach items="${list}" var="dept">
            <tr>
                <td><input type="checkbox" value="${dept.id}" class="cb"/></td>
                <td>${dept.id}</td>
                <td>${dept.name}</td>
                <td>${dept.address}</td>
                <td>${dept.phone}</td>
                <td><a class="btn btn-xs btn-danger" href="javascript:deleteById(${dept.id})">删除</a></td>
                <td><a class="btn btn-xs btn-primary" href="/dept?method=toUpdate&id=${dept.id}">编辑</a></td>
            </tr>
        </c:forEach>

    </table>

    <script>
        $(function (){
            $('#checkAll').click(function (){
                $('.cb').prop('checked', this.checked);
            });
        })

        function deleteAll() {
            layer.confirm('您确认要删除么？', function() {
                let array = new Array();
                //遍历所有加了class="cb"的checkbox，把value值放到数组
                $('.cb:checked').each(function() {
                    array.push($(this).val());
                })

                location.href = '/dept?method=deleteAll&ids=' + array;
            });
        }

        function deleteById(id) {
            // let isDelete = confirm('您确认要删除么')
            // if (isDelete) {
            //     location.href = '/dept?method=deleteById&id=' + id;
            // }


            layer.confirm('您确认要删除么？', function() {
                location.href = '/dept?method=deleteById&id=' + id;
            });


        }
    </script>
</body>
</html>
